function parseData(){ 
	$.ajax({ "url":"/oil/", "error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"p":getQ().p},
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				window.DATA=data
				$("#price").text("￥")
				$("#site").text("川渝南方石油"+data.site)
				$("#address").text(data.address)
				if (data.img!==""){
					$("#siteImg").attr("src",data.img)
				}
				//生成燃油列表
				var select=document.getElementById("oilList")
				var _i=0
				for(var oil in data.price){
					_i+=1
					oil=oil.toUpperCase()

					var div1=document.createElement("div")
					div1.style.cssText="border-radius:5px;border:1px solid green;padding:10px;text-align:center;background:white;margin-top:10px"
						div1.className="NUM col-xs-3 col-xs-offset-1"
					var div2=document.createElement("div")
					div2.style.cssText="font-weight:bold;"
					div2.setAttribute("num",oil.toLowerCase())
					div2.textContent=oil+" #"
					div1.appendChild(div2)
					select.appendChild(div1)
				
				}
			}
		}
	})
}
$(document).on("click",".NUM",function(){
		var oil=$(this).find("div").eq(0).attr("num")
		$("#oilList>div").css("background","white").attr("selected","no")
		this.style.background="#b8e5b8"
		this.setAttribute("selected","yes")
		if(oil===""){
			reset()
		}
		else{
			reset()
			var price=window.DATA.price[oil]
			$("#price").text("￥"+price).attr("price",price)
			$("#oil-n").text(oil.toUpperCase()+"#")
		}
})
function reset(){
	$("#amount").val("")
	$("#amount2").text("￥0")
	$("#capacity").text("大约0L").attr("value","0")
	$("#capacity2").text("0.00L")
	$("#jifen").text("本次加油VIP可积0积分")
	$("#free").text("￥0")
	$("#realPay").text("￥0")
	$("#realPay2").text("￥0")
	$("#realPay3").text("￥0")
	$("#price").text("￥").attr("price","")
	$(".p>div[1]").text("优惠￥0")
	$("#oil-n").text("")
}
function change(){
	var amount=$("#amount").val()
	try{
		amount=parseFloat(amount).toFixed(2)
		if(isNaN(amount)){
			amount=0
		}
	}
	catch(e){
		reset()
		alert("您只能输入数字和小数点！")
		return false;
	}
	var price=$("#price").attr("price")
	try{
		price=parseFloat(price)
		if(isNaN(price)){
			reset()
			alert("请先选择油号！")
			return false;
		}
	}
	catch(e){
		reset()
		alert("请先选择油号！")
		return false;
		
	}
	var capacity=(amount/price).toFixed(2)
	$("#capacity").text("大约"+capacity+"L").attr("value",capacity)
	$("#capacity2").text(capacity+" L")
	goFree()
	
}
document.getElementById("amount").onkeyup=function(){
	change()
	
}
$(document).on("click",".P",function(){
	$(".P").css("background","white")
	$(this).css("background","#f8dace")
	var amount=$(this).find("div").eq(0).attr("amount")
	$("#amount").val(amount)
	change()
})


function goFree(){
	//计算优惠情况
	window.TMP={}
	window.TMP.p=getQ().p
	window.TMP.amount=parseFloat($("#amount").val()).toFixed(2)
	if (isNaN(window.TMP.amount)){
		window.TMP.amount=0
	}
	$("#amount2").text("￥"+window.TMP.amount)
	window.TMP.price=parseFloat($("#price").attr("price")).toFixed(2)
	window.TMP.oil=$("#oilList div[selected='yes']").find("div").eq(0).attr("num")
	console.log(window.TMP)
	if (window.TMP.oil===undefined){
		alert("请选择油号！")
		return false;
	}
	if(/red/.test(document.getElementById("zhifu_pay").style.border)){
		window.TMP.method="zhifu"
	}
	if(/red/.test(document.getElementById("weichat_pay").style.border)){
		window.TMP.method="weichat"
	}
	if(/red/.test(document.getElementById("cash_pay").style.border)){
		window.TMP.method="cash"
	}
	if(/red/.test(document.getElementById("member_pay").style.border)){
		window.TMP.method="online"
	}
	
		//汽油
		if(window.TMP.oil==="92" || window.TMP.oil==="95"){
			for (var i=0;i<window.DATA.free.length;i++){
				if(parseFloat(window.DATA.free[i].first) <=window.TMP.amount && window.TMP.amount<=parseFloat(window.DATA.free[i].last) && window.TMP.oil===window.DATA.free[i].oil ){
					if (window.TMP.method==="weichat" || window.TMP.method==="zhifu"){
						var site_free=parseFloat((window.DATA.site_free * window.TMP.amount).toFixed(2))
						var realPay=(window.TMP.amount * (1-window.DATA.free[i].free/100.0) -site_free).toFixed(2)
						var free=(window.TMP.amount * (window.DATA.free[i].free/100.0)).toFixed(2)
					}
					else if(window.TMP.method==="cash"){
						var site_free=0
						var realPay=window.TMP.amount
						var free="0.00"
					}
					else if(window.TMP.method==="online"){
						var site_free=parseFloat((window.DATA.site_free * window.TMP.amount).toFixed(2))
						var realPay=(window.TMP.amount * (1-window.DATA.free[i].member_free/100.0)-site_free).toFixed(2)
						var free=(window.TMP.amount * (window.DATA.free[i].member_free/100.0)).toFixed(2)
					}
					$("#realPay").text("￥"+realPay)
					$("#realPay2").text("￥"+realPay)
					$("#realPay3").text("￥"+realPay)
					$("#free").text("￥"+(parseFloat(free)+site_free).toFixed(2))
					$("#jifen").text("本次加油VIP可积"+realPay+"积分")
					//明细部分
					$("#free-d").text("￥"+free)
					$("#site-free").text("￥"+site_free)
					$("#jifen-b").text(realPay+" 积分")
					break;//中断，下面的else就不会运行了
				}
				else{
					if(window.TMP.method==="cash"){
						var realPay=window.TMP.amount
						var free="0.00"
						var site_free=0
					}
					else{
						var realPay=window.TMP.amount
						var free="0.00"
						var site_free=(window.DATA.site_free * window.TMP.amount).toFixed(2)
					}
					$("#realPay").text("￥"+realPay)
					$("#realPay2").text("￥"+realPay)
					$("#realPay3").text("￥"+realPay)
					$("#free").text("￥"+free)
					$("#jifen").text("本次加油VIP可积"+realPay+"积分")
					//明细部分
					$("#free-d").text("￥0")
					$("#site-free").text("￥"+site_free)
					$("#jifen-b").text(realPay+" 积分")
				}
			}
		}
		else{
			//针对0，LNG,电的优惠
			for (var i=0;i<window.DATA.free.length;i++){
				if(parseFloat(window.DATA.free[i].first) <=window.TMP.amount && window.TMP.amount<=parseFloat(window.DATA.free[i].last) && window.TMP.oil.toUpperCase()===window.DATA.free[i].oil.toUpperCase() ){
					if (window.TMP.method==="weichat" || window.TMP.method==="zhifu"){
						var capacity=$("#capacity").attr("value")
						var realPay=0
						if(capacity<=0){
							capacity=0
						}
						var free=(capacity * window.DATA.free[i].free).toFixed(2)
						var realPay=(window.TMP.amount- free).toFixed(2)
					}
					else if(window.TMP.method==="cash"){
						var realPay=window.TMP.amount
						var free="0.00"
					}
					else if(window.TMP.method==="online"){
						var capacity=$("#capacity").attr("value")
						var realPay=0
						if(capacity<=0){
							capacity=0
						}
						var free=(capacity * window.DATA.free[i].member_free).toFixed(2)
						var realPay=(window.TMP.amount- free).toFixed(2)
					}
					$("#realPay").text("￥"+realPay)
					$("#realPay2").text("￥"+realPay)
					$("#realPay3").text("￥"+realPay)
					$("#free").text("￥"+free)
					//明细部分
					$("#free-d").text("￥"+free)
					$("#jifen-b").text("0 积分")
					break;//中断，下面的else就不会运行了
				}
				else{
					$("#realPay").text("￥"+window.TMP.amount)
					$("#realPay2").text("￥"+window.TMP.amount)
					$("#realPay3").text("￥"+window.TMP.amount)
					$("#free").text("￥"+0)
					$("#jifen").text("0 积分")
					//明细部分
					$("#free-d").text("￥0")
					$("#jifen-b").text("0 积分")
				}
			}
		}
}





document.getElementById("pay").onclick=function(){
	if (window.TMP===undefined){
		alert("请选择油号！")
		return false;
	}
	if(window.TMP.amount<=0){
		alert("请输入消费金额！")
	}
	var amount=$("#amount").val()
	if(! /^[0-9\.]+$/.test(amount)){
		alert("请输入消费金额！")
		return false;
	}
	if(/red/.test(document.getElementById("zhifu_pay").style.border)){
		window.TMP.method="zhifu"
		//选择了支付宝
		if(!/AlipayClient/.test(window.navigator.userAgent)){
			alert("请使用支付宝扫码下单！")
			return false;
		}
	}
	if(/red/.test(document.getElementById("weichat_pay").style.border)){
		window.TMP.method="weichat"
		//选择了微信
		if(!/MicroMessenger/.test(window.navigator.userAgent)){
			alert("请使用微信扫码下单！")
			return false;
		}
	}
	if(/red/.test(document.getElementById("cash_pay").style.border)){
		window.TMP.method="cash"
		$("#shadow3").show()
		$("#pass-div").show()
		return true
	}
	if(/red/.test(document.getElementById("member_pay").style.border)){
		window.TMP.method="online"
		$("#shadow3").show()
		$("#m-div").show()
		return true
	}
	goPay()
}
document.getElementById("login").onclick=function(){
	var _data={}
	_data.username=$("#username").val()
	_data.password=$("#password").val()
	if(/^ *$/.test(_data.username)){
		alert("请输入用户名！")
		return false;
	}
	if(/^ *$/.test(_data.password1)){
		alert("请输入密码！")
		return false;
	}
	$.ajax({
		"url":"/cheungssh/login/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"username":_data.username,"password":_data.password},
		"type":"POST",
		"success":function(data){
			data=JSON.parse(data)
			if(responseCheck(data)){
				if (data.type==="会员"){
					if(data.notice===true){
						alert(data.content)
						$("#main").hide()
						//$("#body").css("background","#cbcbce")
						$("#body").css("background","white")
						$("#d5").show()
						$("#tel").val(_data.username)
						return false;
					}
					goPay();
				}
				else{
					showErrorInfo("无效登录！")
				}
			}
		}
	})
}
document.getElementById("commit").onclick=function(){
	var data={}
	data.chinese_name=$("#name").val()
	data.number=$("#number").val()
	if (/^ *$/.test(data.chinese_name)){
		alert("姓名不能为空哦！")
		return false;
	}
	if(/^ *$/.test(data.number)){
		alert("车牌号不能为空哦！")
		return false;
	}
	data=JSON.stringify(data)
	$.ajax({
		"url":"/complete_name/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"type":"POST",
		"data":{"data":data},
		"success":function(data){
			data=JSON.parse(data)
			if(responseCheck){
				alert("修改成功，正在为您扣费,请稍等")
				$("#m-div").hide()
				$("#d5").hide()
				$("#main").show()
				goPay()
			}
		}
	})
}
document.getElementById("save").onclick=function(){
	window.TMP.password=$("#password2").val()
	goPay()
}
function goPay(){
	$.ajax({
		"url":"/check_pay/",
		"error":errorAjax,
		"data":{"data":JSON.stringify(window.TMP)},
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"type":"POST",
		"success":function(data){
			data=JSON.parse(data)
			if(responseCheck(data)){
				if(window.TMP.method==="weichat" || window.TMP.method==="zhifu"){
					window.location.href=data.content
				}
				else{
					//这样做就不能回退了
					if(data.real_pay===undefined){
						var banance=0
						var real_pay=window.TMP.amount
					}
					else{
						var banance=data.banance
						var real_pay=data.real_pay
					}
					window.location.replace("./welcom.html?banance="+banance+"&real_pay="+real_pay)
					//window.location.href="./welcom.html"
				}
			}
			else{
				$("#m-div").hide()
				$("#password").val("")
				$("#password2").val("")
			}
		}
	})
}
$(document).on("click","#shadow3",function(){
	document.getElementById("detail").style.display="none";
	document.getElementById("shadow3").style.display="none";
	document.getElementById("jiantou").className="glyphicon glyphicon-chevron-up";
})
document.getElementById("detail-b").onclick=function(){
	if($("#jiantou").hasClass("glyphicon-chevron-up")){
		document.getElementById("jiantou").className="glyphicon glyphicon-chevron-down";
		document.getElementById("detail").style.display="block";
		document.getElementById("shadow3").style.display="block";
	}
	else{
		document.getElementById("jiantou").className="glyphicon glyphicon-chevron-up";
		document.getElementById("detail").style.display="none";
		document.getElementById("shadow3").style.display="none";
	}
	
}
$(document).on("click",".close-a",function(){
	document.getElementById("jiantou").className="glyphicon glyphicon-chevron-up";
	document.getElementById("detail").style.display="none";
	document.getElementById("shadow3").style.display="none";
	
})
document.getElementById("member_pay").onclick=function(){
	$("#weichat_pay").css("border","#d7c7c7 1px solid ")
	$("#zhifu_pay").css("border","#d7c7c7 1px solid")
	$("#cash_pay").css("border","#d7c7c7 1px solid")
	$("#member_pay").css("border","red 1px solid")
	goFree()
}
document.getElementById("weichat_pay").onclick=function(){
	if(/MicroMessenger/.test(window.navigator.userAgent)){
		//微信客户端
	}
	else{
		alert("请使用微信扫码下单！")
	}
	$("#weichat_pay").css("border"," 1px solid red")
	$("#zhifu_pay").css("border","#d7c7c7 1px solid")
	$("#cash_pay").css("border","#d7c7c7 1px solid")
	$("#member_pay").css("border","#d7c7c7 1px solid")
	goFree()
}
document.getElementById("zhifu_pay").onclick=function(){
	if(/AlipayClient/.test(window.navigator.userAgent)){
		//支付宝app
	}
	else{
		alert("请使用支付宝扫码下单！")
	}
	$("#weichat_pay").css("border","1px solid #d7c7c7")
	$("#zhifu_pay").css("border","red 1px solid")
	$("#cash_pay").css("border","#d7c7c7 1px solid")
	$("#member_pay").css("border","#d7c7c7 1px solid")
	goFree()
}
document.getElementById("cash_pay").onclick=function(){
	$("#weichat_pay").css("border","#d7c7c7 1px solid")
	$("#zhifu_pay").css("border","1px solid#d7c7c7")
	$("#member_pay").css("border","#d7c7c7 1px solid")
	$("#cash_pay").css("border","1px solid red")
	/*
	$("#jifen").text("本次加油VIP可积0积分")
	$("#free").text("￥0")
	$("#realPay").text("￥"+window.TMP.amount)
	$("#realPay2").text("￥"+window.TMP.amount)
	$("#realPay3").text("￥"+window.TMP.amount)
	$(".p>div[1]").text("优惠￥0")
	$("#free-b").text("￥0")
	*/
	goFree()
}
function fullS(){
	allHeight=window.innerHeight;//屏幕高度
	var mainHeight=document.getElementById("main").offsetHeight+60;//现在占用的高度
	var d2=document.getElementById("d2")
	var d3=document.getElementById("d3")
	if(mainHeight<allHeight-2){
		var diff=allHeight-mainHeight
		d2.style.height=(d2.offsetHeight+diff/2) + "px"
		d3.style.height=(d3.offsetHeight+diff/2) + "px"
	}

}
$(document).on("click",".CL",function(){
	this.parentNode.parentNode.style.display="none";
	$("#shadow3").hide("fast")
})
fullS()
parseData()
